package com.kingyun.gpsinspection.purificationservice.common.utils;
import com.cqhilink.api.common.utils.date.DateStyle;
import com.cqhilink.api.common.utils.date.DateUtil;
import com.cqhilink.api.common.utils.enums.RespCodeEnum;
import com.kingyun.gpsinspection.purificationservice.common.constant.BackMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by YuPeng on 2017/1/14.
 * 将用的比较多的方法抽取出来的共通方法
 */
public class CommonUtil {
    private static Logger logger = LoggerFactory.getLogger(CommonUtil.class);
    /**
     * 记录日志及返回统一错误信息
     * @param e
     * @return
     */
    public static Map<String,Object> logError(String error,Exception e){
        Map<String, Object> map = new HashMap<>();
        logger.error(BackMessage.SYSTEM_INNER_ERROR+e.getLocalizedMessage(),e);
        map.put("state", RespCodeEnum.RESP_CODE_ENUM_false.code());
        map.put("msg", BackMessage.SYSTEM_INNER_ERROR);
        return map;
    }
    /**
     * 获取当前时间格式化字符串
     * @return
     */
    public static String getTimeString() {
        return DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM);
    }

    /**
     * 用于格式化从oracle数据库中获取的时间带有毫秒的，去掉毫秒
     * @return
     */
    public static String formatDate(String dateString){
        if(dateString!=null && dateString.length()>19){
            return dateString.substring(0,dateString.length()-2);
        }
        return dateString;
    }
}
